package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.HoOccupancyInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HoRepairApplication;
import com.ruoyi.system.service.IHoRepairApplicationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报修申请Controller
 *
 * @author ruoyi
 * @date 2025-01-10
 */
@RestController
@RequestMapping("/system/repairApplication")
public class HoRepairApplicationController extends BaseController {
    @Autowired
    private IHoRepairApplicationService hoRepairApplicationService;

    /**
     * 查询报修申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:repairApplication:list')")
    @GetMapping("/list")
    public TableDataInfo list(HoRepairApplication hoRepairApplication) {
        startPage();
        if (null != hoRepairApplication.getStudentId() && hoRepairApplication.getStudentId() == 0) {
            hoRepairApplication.setStudentId(null);
        } else {
            hoRepairApplication.setStudentId(getUserId());
        }
        List<HoRepairApplication> list = hoRepairApplicationService.selectHoRepairApplicationList(hoRepairApplication);
        return getDataTable(list);
    }

    /**
     * 导出报修申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:repairApplication:export')")
    @Log(title = "报修申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HoRepairApplication hoRepairApplication) {
        if (null != hoRepairApplication.getStudentId() && hoRepairApplication.getStudentId() == 0) {
            hoRepairApplication.setStudentId(null);
        } else {
            hoRepairApplication.setStudentId(getUserId());
        }
        List<HoRepairApplication> list = hoRepairApplicationService.selectHoRepairApplicationList(hoRepairApplication);
        ExcelUtil<HoRepairApplication> util = new ExcelUtil<HoRepairApplication>(HoRepairApplication.class);
        util.exportExcel(response, list, "报修申请数据");
    }

    /**
     * 获取报修申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:repairApplication:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(hoRepairApplicationService.selectHoRepairApplicationById(id));
    }

    /**
     * 新增报修申请
     */
    @PreAuthorize("@ss.hasPermi('system:repairApplication:add')")
    @Log(title = "报修申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HoRepairApplication hoRepairApplication) {
        hoRepairApplication.setStudentId(getUserId());
        hoRepairApplication.setName(getLoginUser().getUser().getNickName());
        hoRepairApplication.setStudentNum(getLoginUser().getUser().getUserName());
        hoRepairApplication.setReportTime(DateUtils.getNowDate());
        hoRepairApplication.setStatus("1");
        hoRepairApplication.setCreateUserId(getUserId());
        return toAjax(hoRepairApplicationService.insertHoRepairApplication(hoRepairApplication));
    }

    /**
     * 修改报修申请
     */
    @PreAuthorize("@ss.hasPermi('system:repairApplication:edit')")
    @Log(title = "报修申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HoRepairApplication hoRepairApplication) {
        HoRepairApplication old = hoRepairApplicationService.selectHoRepairApplicationById(hoRepairApplication.getId());
        if (!"1".equals(old.getStatus())) {
            throw new RuntimeException("不是未处理状态的不能进行操作！");
        }
        if (null == old.getDormitoryId()) {
            throw new RuntimeException("报修宿舍不能为空！");
        }
//        if ("4".equals(hoRepairApplication.getStatus())) {
//            // 撤回
//        } else if ("2".equals(hoRepairApplication.getStatus())) {
//            // 已处理
//        } else if ("3".equals(hoRepairApplication.getStatus())) {
//            // 无法处理
//        }
        return toAjax(hoRepairApplicationService.updateHoRepairApplication(hoRepairApplication));
    }

    /**
     * 删除报修申请
     */
    @PreAuthorize("@ss.hasPermi('system:repairApplication:remove')")
    @Log(title = "报修申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(hoRepairApplicationService.deleteHoRepairApplicationByIds(ids));
    }
}
